/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package gui.settings;

import domain.DomainController;
import domain.enums.Protection;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 */
public class PasswordPanel extends javax.swing.JPanel
{
    /** Creates new form PasswordPanel */
    public PasswordPanel()
    {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblLock = new javax.swing.JLabel();
        lblInfo = new javax.swing.JLabel();
        cmdPassword = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        lblLock.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/lock_open.png"))); // NOI18N
        add(lblLock, java.awt.BorderLayout.LINE_START);

        lblInfo.setText("<html>You can password protect your history. Click the 'Lock' button to set your password.</html>");
        add(lblInfo, java.awt.BorderLayout.CENTER);

        cmdPassword.setText("Lock");
        cmdPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdPasswordActionPerformed(evt);
            }
        });
        add(cmdPassword, java.awt.BorderLayout.LINE_END);
    }// </editor-fold>//GEN-END:initComponents

    private void cmdPasswordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdPasswordActionPerformed

        DomainController.getInstance().setProtection(
                    (this.cmdPassword.getText().equals("Lock")) ? Protection.LOCK : Protection.UNLOCK
                );

    }//GEN-LAST:event_cmdPasswordActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdPassword;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JLabel lblLock;
    // End of variables declaration//GEN-END:variables


    public void updatePasswordGUI()
    {
        boolean locked = DomainController.getInstance().isLocked();
        
        lblLock.setIcon(
                (locked) ?
                new javax.swing.ImageIcon(getClass().getResource("/data/lock_closed.png"))
                    :
                new javax.swing.ImageIcon(getClass().getResource("/data/lock_open.png"))
                );

        lblLock.setToolTipText(
                (locked) ?
                "Your history is locked!"
                    :
                "Your history is not locked!"
                );

        lblInfo.setText(
                (locked) ?
                "<html>You can password protect your history. Click the 'Unlock' button to remove the password.</html>"
                    :
                "<html>You can password protect your history. Click the 'Lock' button to set your password.</html>"
                );

        cmdPassword.setText(
                (locked) ?
                "Unlock"
                    :
                "Lock"
                );
    }

    public void setButtonsEnabled(boolean enabled)
    {
        this.cmdPassword.setEnabled(enabled);
    }
}